package com.graduation_design.modules.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.graduation_design.common.lang.Result;
import com.graduation_design.entity.AppCategory;
import com.graduation_design.entity.AppProduct;
import com.graduation_design.modules.app.controller.BaseController;
import com.graduation_design.service.AppCategoryService;
import com.graduation_design.service.AppProductService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;

@RestController
@RequestMapping("/admin/category")
public class AdminCategoryController extends BaseController {

    @Resource
    AppCategoryService appCategoryService;

    @Resource
    AppProductService appProductService;

    @GetMapping("/list")
//    @RequiresPermissions("admin:category:list")
    public Result list(String name) {
        return Result.success(appCategoryService.page(getPage(),
                new QueryWrapper<AppCategory>()
                        .like(StrUtil.isNotBlank(name), "name", name)
                        .orderByDesc("created")
        ));
    }

    @GetMapping("/info/{id}")
//    @RequiresPermissions("admin:category:list")
    public Result info(@PathVariable Long id) {
        return Result.success(appCategoryService.getById(id));
    }

    @PostMapping("/save")
//    @RequiresPermissions(value = {"admin:category:save", "admin:category:update"}, logical = Logical.OR)
    public Result save(@Validated @RequestBody AppCategory appCategory) {

        if (appCategory.getId() == null) {
            appCategory.setCreated(LocalDateTime.now());
        } else {
            appCategory.setUpdated(LocalDateTime.now());
        }

        appCategoryService.saveOrUpdate(appCategory);
        return Result.success(appCategory);
    }

    @PostMapping("/delete")
//    @RequiresPermissions("admin:category:delete")
    public Result delete(@RequestBody Long[] ids) {
        long count = appProductService.count(new QueryWrapper<AppProduct>()
                .in("category_id", Arrays.asList(ids))
        );
        if (count > 0) {
            return Result.fail("该分类下有关联商品，不允许删除");
        }

        appCategoryService.removeByIds(Arrays.asList(ids));
        return Result.success();
    }
}
